package lesson05.blocking_queue;



import java.util.concurrent.TimeUnit;

/**
 * 功能描述
 *
 * @author wileda
 * @date 2023/01/13  14:06
 */
public class Demo_BlockingQueue03 {
    public static void main(String[] args) {
        MyBlockingQueue queue = new MyBlockingQueue();
        //创建生产者
        Thread producer = new Thread(() ->{
            int num = 0;
            while(true){
                try {
                    queue.put(num);
                    System.out.println("生产了元素 " + num);
                    num++;
                    TimeUnit.MILLISECONDS.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        producer.start();

        Thread consumer = new Thread(() ->{
            while (true) {
                try {
                    Integer result = queue.take();
                    System.out.println("消费了元素 " + result);
                    TimeUnit.MILLISECONDS.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        consumer.start();
    }
}
